    //
//  SettingLayoutViewController.m
//  PhotoFrame
//
//  Created by William on 11-5-7.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SettingLayoutViewController.h"
#import "GlobalSettingUtils.h"


@implementation SettingLayoutViewController
@synthesize listData;
@synthesize lastIndexPath;
@synthesize delegate;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
								   initWithTitle:@"Done" 
								   style:UIBarButtonItemStyleBordered 
								   target:self action:@selector(closePop)];
	self.navigationItem.rightBarButtonItem = doneButton;
	[doneButton release];
	
	NSArray *array = [[NSArray alloc] initWithObjects:@"下フレーム", @"右フレーム", @"フレームなし", @"グミフレーム", nil];
	self.listData = array;
	[array release];
	
    [super viewDidLoad];
}

- (void) closePop {
	[delegate closePopover];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"設定";
}

- (NSInteger)numberOfSections {
    return 1;
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
	return [self.listData count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *identify = @"SimpleTableIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] 
				 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify] autorelease];
	}
	
	NSUInteger index = [indexPath row];

	// load properties from user document folder
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* sandBoxpath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,@"globalconf.plist"];
	NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:sandBoxpath];
	
	// load from app default resources folder
	if (dict == nil) {
		NSString *path = [[NSBundle mainBundle] pathForResource:@"globalconf" ofType:@"plist"];
		dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
	}
	
	NSUInteger row = [indexPath row];
	NSUInteger oldRow = [lastIndexPath row];
	cell.textLabel.text = [listData objectAtIndex:row];
	NSString *layout = [dict objectForKey:@"layout"];
	cell.accessoryType = (row == oldRow && lastIndexPath != nil || [layout intValue] == index + 1) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
	if ([layout intValue] == index + 1) {
		lastIndexPath = indexPath;
	}
	[dict release];
	
	return cell;
}

#pragma mark -
#pragma mark Table Delegate Methods
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	int newRow = [indexPath row];
	int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
	
	// save change to global files
	[GlobalSettingUtils setGlobalConfForValue:[NSNumber numberWithInt: newRow + 1] forKey:@"layout"];
	
	if (newRow != oldRow) {
		UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
		newCell.accessoryType = UITableViewCellAccessoryCheckmark;
		
		UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
		oldCell.accessoryType = UITableViewCellAccessoryNone;
		lastIndexPath = indexPath;
	}
	
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	[delegate initViewOrder:newRow + 1];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.listData = nil;
	self.lastIndexPath = nil;
}


- (void)dealloc {
	[listData release];
	[lastIndexPath release];
    [super dealloc];
}


@end
